package com.taobaolite.user;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.taobaolite.transaction.Transaction;
import com.taobaolite.user.admin.Admin;
import com.taobaolite.user.guest.Guest;
import com.taobaolite.user.store.Store;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import javax.persistence.*;
import javax.validation.constraints.Min;
import java.util.Set;

@Entity
@Table(name = "user")
public class User {

    @Id
    @SequenceGenerator(
            name = "user_id_generator",
            sequenceName = "user_id_sequence",
            initialValue = 900000000, // 9 digits
            allocationSize = 5 // 这个间隔是每次重新启动服务器会增加的间隔，似乎，重启几次是012 678这样
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "user_id_generator"
    )
    private Long userId;

    @Column(nullable = false)
    private String username;

    @Column(nullable = false)
    private String password;

    @Column(nullable = false)
    @Min(value = 0)
    private Double balance;

    @Column(nullable = false, updatable = false)
    private UserType type;

    @Column(unique = true, nullable = false)
    private String email;

    @OneToOne (cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    private Guest guest;
    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    private Store store;
    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    private Admin admin;

    @OneToMany(cascade = CascadeType.ALL)
    @OnDelete(action = OnDeleteAction.NO_ACTION)
    @JoinColumn(name = "userFrom")
    private Set<Transaction> userFrom;

    @OneToMany(cascade = CascadeType.ALL)
    @OnDelete(action = OnDeleteAction.NO_ACTION)
    @JoinColumn(name = "userTo")
    private Set<Transaction> userTo;

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }

    public void setType(UserType type) {
        this.type = type;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setGuest(Guest guest) {
        this.guest = guest;
    }

    public void setStore(Store store) {
        this.store = store;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public Long getUserId() {
        return userId;
    }

    public String getPassword() {
        return password;
    }

    public Double getBalance() {
        return balance;
    }

    public UserType getType() {
        return type;
    }

    public String getEmail() {
        return email;
    }

    public Guest getGuest() {
        return guest;
    }

    public Store getStore() {
        return store;
    }

    public Admin getAdmin() {
        return admin;
    }

    public Set<Transaction> getUserFrom() {
        return userFrom;
    }

    public void setUserFrom(Set<Transaction> userFrom) {
        this.userFrom = userFrom;
    }

    public Set<Transaction> getUserTo() {
        return userTo;
    }

    public void setUserTo(Set<Transaction> userTo) {
        this.userTo = userTo;
    }

    public User(String username, String password, Double balance, UserType type, String email) {
        this.username = username;
        this.password = password;
        this.balance = balance;
        this.type = type;
        this.email = email;
    }
    public static void checkToken(){
        try {
            StpUtil.checkLogin();
        } catch (
                NotLoginException e) {
            throw new ResponseStatusException(
                    HttpStatus.UNAUTHORIZED,
                    "User not logged in!"
            );
        }
    }

    public User() {}
}
